<?php
/*
	用户端公共文件，访问用户端时默认加载
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'index';
$areasetting=$this->plugin_setting('areasetting');
if($_RQ['op']=='index'){
	//还不是分销商，跳转申请页
	$return['apply']=$this->get('area_agent',array('mid'=>$_SESSION['uid']));
	$return['member']=$this->get('member',array('mid'=>$_SESSION['uid']));
	$return['become']=$areasetting['become'];
	$return['page_setting']=$this->plugin_setting('areapage');
	$return['apply_setting']=$this->plugin_setting('areaapply');
	if(!$areasetting['no_indexurl']){
		$return['index_url']=$areasetting['index_url']?:'jy_guashop/index/';
	}
	$return['item']=model('core/member')->detail($_SESSION['uid'],array('nickname','avatar','realname','mobile','wechat'));	
	$return['item']['oldmobile']=$return['item']['mobile'];
	$setting=uni_setting('register');
	$return['item']['login_type']=$setting['login_type'];
	$return['item']['area_type']=$return['item']['area_type']?:'4';
	$return['agent_level']=$areasetting['level'];
	$return['item']['storeid']=$return['stores'][0]['id'];
	$return['province']=cfc('tools')->pcd();
	$return['area_types']=$this->area_types;
	set_html('title',$return['apply_setting']['html_title']?:'申请入驻');
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['agree']){
		exi('请先阅读协议！','error');
	}
	if(!$post['realname']){
		exi('请输入姓名！','error');
	}
	if(!$post['mobile']){
		exi('请输入联系电话！','error');
	}
	if($post['login_type']>0 && $post['oldmobile']!=$post['mobile'] && !check_sms_code($post['smscode'])){
		$oldmobile=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),'mobile');
		if($post['mobile']!=$oldmobile){
			exi('手机验证码错误！','error');
		}
	}
	//代理地区整理
	if(!$post['area'] || !$post['area']['province']){
		exi('请选择代理地区！','error');
	}
	$areasetting=$this->plugin_setting('areasetting');
	$becomeset=$areasetting['becomeset'];
	if($areasetting['become']==3){
		$ispass=0;
		$member=$this->get('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		foreach($becomeset as $bv){
			if($bv['area_type']==$post['area_type'] && ($bv['level_id']==$member['level_id'] || !$bv['level_id'])){
				$ispass=1;
			}
		}
		if(!$ispass){
			exi('当前团队等级，不能申请该级别的区域代理！','error');
		}
	}
	$area_code=$this->model('area')->get_area_code($post['area']);
	$this->model('area')->apply_agent($_SESSION['uid'],$area_code,$post);
	exi('申请成功！请耐心等待管理员审核！','','/area.apply/');
}
